package reader;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

/**
 * this class performs the function of locating the file(s) that the user is
 * looking for and also contains the GUI part of the app
 * 
 * @author naveen
 * 
 */
public class Folderlocator {

	private String Search_folder;
	private String Search_criteria;
	private boolean Search_ext = false;
	private boolean Search_name = false;
	private boolean Folder_validity;
	JFrame myFrame;
	JTextArea text;
	JTextArea Notification;
	JPanel bottom;
	JPanel top;
	JPanel middle;
	JLabel attention;
	JMenuBar menubar;
	JMenu menu1;
	JMenu menu2;
	ReadingMachine rmobj;

	public Folderlocator() {
		Search_folder = "Please select the folder to search Within....";
		Folder_validity = false;
		rmobj = new ReadingMachine();
	}

	public String getSearch_folder() {
		return Search_folder;
	}

	public void setSearch_folder(String search_folder) {
		Search_folder = search_folder;
	}

	/**
	 * get the validity of the Search folder
	 * 
	 * @return validity of the folder
	 */
	public boolean getFolder_Validity() {
		return Folder_validity;
	}

	/**
	 * set the value of the Search folder
	 * 
	 * @param validity
	 */
	public void setFolder_Validity(boolean validity) {
		Folder_validity = validity;
	}

	public String getSearch_criteria() {
		return Search_criteria;
	}

	public void setSearch_criteria(String search_criteria) {
		Search_criteria = search_criteria;
	}

	public void checkFolderValidity(String fileName) {

		File folder = new File(Search_folder);
		if (folder.isDirectory()) {
			Folder_validity = true;
		}
	}

	public boolean isSearch_ext() {
		return Search_ext;
	}

	public void setSearch_ext(boolean search_ext) {
		Search_ext = search_ext;
	}

	public boolean isSearch_name() {
		return Search_name;
	}

	public void setSearch_name(boolean search_name) {
		Search_name = search_name;
	}

	public void launchGUI() {

		myFrame = new JFrame("FILE READER V1.0");
		top = new JPanel(new BorderLayout());
		// middle = new JPanel(new GridBagLayout());
		middle = new JPanel(new BorderLayout());
		bottom = new JPanel(new GridBagLayout());
		// top = new JPanel(new GridLayout(2,2));
		// bottom = new JPanel(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(4, 4, 8, 8);

		myFrame.setSize(550, 550);
		myFrame.setVisible(true);
		myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		menubar = new JMenuBar();
		menu1 = new JMenu("File");
		JMenuItem menuItem = new JMenuItem("Open");
		JMenuItem menuItem2 = new JMenuItem("Close");
		JMenuItem menuItem3 = new JMenuItem("Exit");
		menu1.add(menuItem);
		menu1.add(menuItem2);
		menu1.add(menuItem3);
		menubar.add(menu1);

		menu2 = new JMenu("Others");
		menubar.add(menu2);

		myFrame.setJMenuBar(menubar);

		attention = new JLabel("Attention");
		top.add(attention, BorderLayout.CENTER);

		text = new JTextArea();
		text.setSize(35, 35);
		text.setText("No Folder Selected");
		top.add(text, BorderLayout.SOUTH);

		myFrame.add(top, BorderLayout.NORTH);

		JLabel info = new JLabel("Notification Area");
		middle.add(info, BorderLayout.NORTH);
		// middle.add(info, gbc);
		Notification = new JTextArea();
		Notification.setText("Welcome!!!");
		middle.add(Notification, BorderLayout.CENTER);
		// middle.add(Notification, gbc);
		myFrame.add(middle, BorderLayout.CENTER);

		JButton sel = new JButton();
		// sel.setSize(5, 10);
		sel.setText("Select");
		sel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				launchFileChooser();
			}
		});

		JButton search = new JButton("Search");
		search.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				launchSearch();
			}
		});

		JButton rename = new JButton("Rename");
		JButton exit = new JButton("EXIT");
		bottom.add(sel, gbc);
		bottom.add(search, gbc);
		bottom.add(rename, gbc);
		bottom.add(exit, gbc);

		myFrame.add(bottom, BorderLayout.SOUTH);
		myFrame.revalidate();

	}

	/**
	 * launches the file chooser and sets the folder path once user selects a
	 * folder
	 * 
	 */
	public void launchFileChooser() {

		JFileChooser file1 = new JFileChooser();
		file1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

		int returnVal = file1.showOpenDialog(null);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File selectedFile = file1.getCurrentDirectory();
			Search_folder = selectedFile.toString();
			text.setText(Search_folder);
		}
	}

	/**
	 * launches the search functionality and displays the list of files matching
	 * the criteria
	 */
	public void launchSearch() {

		// defining the frame for the search function
		final JFrame search_frame = new JFrame("Search the Folder");
		search_frame.setVisible(true);
		search_frame.setSize(300, 400);
		search_frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

		JPanel search_p1 = new JPanel(new GridLayout(4, 1));
		// JPanel search_p1 = new JPanel(new GridBagLayout());
		// GridBagConstraints gbc2 = new GridBagConstraints();
		// gbc2.insets = new Insets(5,5,5,5);

		final JTextField txt1 = new JTextField("eg: .mp3");
		txt1.setEnabled(false);
		final JTextField txt2 = new JTextField("eg: bunny");
		txt2.setEnabled(false);
		JCheckBox chk1 = new JCheckBox("Search by Extension");
		chk1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (txt1.isEnabled()) {
					txt1.setEnabled(false);
					Search_ext = false;
				} else {
					txt1.setEnabled(true);
					Search_ext = true;
				}
			}
		});
		JCheckBox chk2 = new JCheckBox("Search by Name");
		chk2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (txt2.isEnabled()) {
					txt2.setEnabled(false);
					Search_name = false;
				} else {
					txt2.setEnabled(true);
					Search_name = true;
				}
			}
		});

		search_p1.add(chk1);
		search_p1.add(txt1);
		search_p1.add(chk2);
		search_p1.add(txt2);

		txt1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Search_criteria = txt1.getText();
			}
		});

		txt1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Search_criteria = txt2.getText();
			}
		});
		

		JPanel search_p2 = new JPanel();
		JButton btn1 = new JButton("OK");		
		btn1.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				
			}
		});
		
		JButton btn2 = new JButton("Cancel");
		btn2.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
			   search_frame.setVisible(false);	
			}
		});
		
		search_p2.add(btn1);
		search_p2.add(btn2);

		JPanel top_pan = new JPanel(new BorderLayout());
		top_pan.add(search_p1, BorderLayout.NORTH);
		top_pan.add(search_p2, BorderLayout.SOUTH);
		search_frame.add(top_pan);
		// setSearch_criteria((String) JOptionPane.showInputDialog(null
		// ,"Enter your search Criteria",
		// "Seach Criteria", JOptionPane.PLAIN_MESSAGE, null , null,
		// "One Tree"));
		rmobj.searchFiles(this);
	}

}
